using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using RS.DOM.Common;
using RS.DOM.Construction;

namespace RSTest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            //rtbProgram.Text = "E Y:=5\r\nE $:=1..Y\r\nE !S:=0..(Y-2)\r\nE $#(I+2):=$#(I+1)+$#I";

            if (File.Exists("program.txt"))
            {
                StreamReader sr = new StreamReader("program.txt");
                rtbProgram.Text = sr.ReadToEnd();
                sr.Close();
            }
            else
            {
                rtbProgram.Text = string.Empty;
            }

            //MessageBox.Show(rtbProgram.Text.Length.ToString());
        }

        private void btRun_Click(object sender, EventArgs e)
        {
            Document.ClearState();

            Document doc = new Document(rtbProgram.Text);
            doc.Build();
            doc.Execute();

            if (!doc.HasError)
            {
                rtbOutput.Text = doc.Output;
                //rtbArea.Text = doc.Area.ToString();
                rtbArea.Text = string.Empty;
            }
            else
            {
                rtbOutput.Text = doc.Error;
                rtbArea.Text = string.Empty;
            }

            StringBuilder sb = new StringBuilder();

            foreach (string text in doc.Log)
            {
                 sb.Append(text + "\r\n");
            }

            rtbArea.Text = sb.ToString();
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            this.Save();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Save();
        }

        private void Save()
        {
            StreamWriter sw = new StreamWriter("program.txt");
            sw.Write(rtbProgram.Text);
            sw.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
                        
        }
    }
}